<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>">
    
    <title>${ webpageTitle }</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	
	<script src="rp/scripts/boot.js" type="text/javascript"></script>
	
	<style type="text/css">
	   	
    </style>
	
  </head>
  
  <body class="body-fit">
  	
  	<div style="width:100%;">
        <div class="mini-toolbar" style="padding:0px;border-top:0;border-left:0;border-right:0;">
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" onclick="addRow()"><i class="fa fa-plus fa-lg"></i> 新增</a>
                        <a class="mini-button" onclick="saveRows()"><i class="fa fa-floppy-o fa-lg"></i> 保存</a>
                        <a class="mini-button mini-button-danger" onclick="removeRows()"><i class="fa fa-trash-o fa-lg"></i> 删除</a>       
                    </td>
                    <td style="white-space:nowrap;">
                    	<input id="name" class="mini-textbox" emptyText="请输入权限名称" onenter="search()"/>
                        <input id="code" class="mini-textbox" emptyText="请输入权限代码" onenter="search()"/>   
                        <a class="mini-button" onclick="search()"><i class="fa fa-search fa-lg"></i> 查询</a>
                    </td>
                </tr>
            </table>           
        </div>
    </div>
    <div class="mini-fit">
	  	<div id="authGrid" class="mini-datagrid" style="width: 100%; height:100%;" borderStyle="border:0;"
	    	url="rp/system/authMng/getAuths" idField="id"
	    	pageSize="20" allowResize="false"
	    	allowCellEdit="true" allowCellSelect="true" multiSelect="true" 
	        editNextOnEnterKey="true" editNextRowCell="true" allowCellValid="true"
	        onshowrowdetail="onShowRowDetail">
		    <div property="columns">
		        <div type="indexcolumn" headerAlign="center">序号</div>
		        <div type="expandcolumn">关联资源</div>
		        <div type="checkcolumn"></div>
		        <div field="name" vtype="required" width="80" headerAlign="center">
		        	权限名称<input property="editor" class="mini-textbox" style="width:100%;"/>
		        </div>    
		        <div field="code" vtype="required" width="120" headerAlign="center">
		        	权限代码<input property="editor" class="mini-textbox" style="width:100%;" emptyText="默认会添加前缀AUTH_"/>
		        </div> 
		        <div field="remark" width="100" headerAlign="center">
		        	备注<input property="editor" class="mini-textarea" style="width:200px;" minWidth="200" minHeight="50"/>
		        </div>
		        <!-- <div name="resources" field="resources" visible="false">
		        	资源
		        </div> -->
		        <div field="resources" width="60" headerAlign="center" renderer="onActionRenderer">
		        	操作
		        </div>
		        <div field="createDate" width="60" headerAlign="center" allowSort="true" renderer="onDateRenderer">
		        	创建日期
		        </div>                
		    </div>
		</div>
		
		<div id="rescGridContainer" style="display: none;">
			<div style="width:100%;">
		        <div class="mini-toolbar" style="border-bottom:0;padding:3px;">
		            <span>关联资源</span>           
		        </div>
		    </div>
			<div id="hasRescGrid" class="mini-datagrid" style="width:100%;height:200px;" 
            	url="rp/system/rescMng/getHasResources" showEmptyText="true" emptyText="没有分配相关资源"
            	showPager="false">
            	<div property="columns">
            		<div type="indexcolumn" headerAlign="center">序号</div>
					<div field="name" width="60" headerAlign="center">资源名称</div>
		        	<div field="url" width="170" headerAlign="center">URL</div>
		        	<div field="remark" width="150" headerAlign="center" >备注</div>
		        	<div field="moduleName" width="50" headerAlign="center">所属模块</div>
		        </div>
			</div>
		</div>
	</div>
  	
	<script type="text/javascript">
		var grid;
		var rescGridContainer;
		var hasRescGrid;
	
		$(function(){
			rescGridContainer = document.getElementById("rescGridContainer");
			
			mini.parse();
			grid = mini.get("authGrid");
			grid.load();
			
			hasRescGrid = mini.get("hasRescGrid");
		});
		
		function search() {
            var name = mini.get("name").getValue();
            var code = mini.get("code").getValue();
            grid.load({ name: name, code: code });
        }
		
		function addRow() {         
            var newRow = { name: "", code: "AUTH_" };
            grid.addRow(newRow, 0);
            grid.beginEditCell(newRow, "name");
        }
		
		function saveRows() {
			grid.validate();
			if (grid.isValid() == false) {
                var error = grid.getCellErrors()[0];
                grid.beginEditCell(error.record, error.column);
                return;
            }
			
            var data = grid.getChanges();
            var json = rp_encode(data);
            grid.loading("保存中，请稍等......");
            rp_ajax("rp/system/authMng/saveAuths", {data: json}, function(text){
            	grid.reload();
            });
        }
		
		function removeRows(){
			var rows = grid.getSelecteds();
			if(rows.length == 0){
				mini.alert("请勾选需要删除的记录...");
			}else{
				mini.confirm("确定删除记录吗？", "友情提示",
		            function (action) {
		                if (action == "ok") {
		                    var json = rp_encode(rows);
		                    if (rows.length > 0) {
		                    	rp_ajax("rp/system/authMng/deleteAuths", {data: json}, function(text){
		                    		grid.reload();
		                        });
		                    }
		                }
		            }
		        );
			}
		}
		
		function onShowRowDetail(e) {
            var grid = e.sender;
            var row = e.record;
            
            var td = grid.getRowDetailCellEl(row);
            td.appendChild(rescGridContainer);
            rescGridContainer.style.display = "block";
			
            hasRescGrid.load({authorityId:row.id});
        }
		
		function onActionRenderer(e){
			var actions = "";
			var record = e.record;
			var id = record.id;
			if(id){
				actions = "<a class='mini-button mini-button-small' onclick='openAllotResourcesWindow()'>分配资源</a>";
			}
			return actions;
		}
		
		function openAllotResourcesWindow(){
			var row = grid.getSelected();
			mini.open({
				url: "rp/system/common/showSelectResourcesWindow?authorityId="+row.id,
				title: "分配资源",
				width: 802,
				height: 560,
				onload: function () {
                	//var iframe = this.getIFrameEl();
                  	//iframe.contentWindow.setSelectData(row);
              	},
				ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
						var data = iframe.contentWindow.getSelectData();
						grid.cancelEdit();
						var row = grid.getSelected();
						grid.updateRow(row, {
							resources: data
						});
                    }
                }
			});
		}
		
		
	</script>
  </body>
</html>
